﻿using System.Collections.Generic;
using UnityEngine;

namespace Script.Data
{
    public class PositionConf
    {
        public static Dictionary<string, PositionConf> PositionConfDict;
        public static List<string> sortLineIdList = new List<string>();
        public string id;
        public string code;
        public string notecn;
        public string noteen;
        public Vector3 position;
        public Vector3 rotation;
        public string previewId;
        public int posType;
        public int sortLineServerId;
        public List<string> showInLeft = new List<string>();

        private static bool inited = false;

        public PositionConf(List<string> conf)
        {
            conf[conf.Count- 1] =conf[conf.Count - 1].Replace("\r","");
            id = conf[0];
            code = conf[1];
            notecn = conf[2];
            noteen = conf[3];
            position = new Vector3(float.Parse(conf[4]), float.Parse(conf[5]), float.Parse(conf[6]));
            rotation = new Vector3(float.Parse(conf[7]), float.Parse(conf[8]), float.Parse(conf[9]));
            previewId = conf[10];
            posType = int.Parse(conf[11]);
            sortLineServerId = int.Parse(conf[12]);
            if (conf[13] != "")
            {
                string[] temp = conf[13].Split('|');
                foreach (var item in temp)
                {
                    showInLeft.Add(item);
                }
            }
            
            if (((ELocationType)posType | ELocationType.E_SortingLine) == ELocationType.E_SortingLine)
            {
                sortLineIdList.Add(id);
            }
            //if (posType == PosType.SortLine)
            //{
            //    sortLineIdList.Add(id);
            //}
        }
        
        public static void Init()
        {
            if (inited)
            {
                return;
            }
            PositionConfDict = new Dictionary<string, PositionConf>();
            inited = true;
            var textAsset = Resources.Load("position") as TextAsset;
            if (textAsset == null)
            {
                return;
            }
            var ret = CSVParser.LoadFromString(textAsset.text);
            for (var i = 1; i < ret.Count; i++)
            {
                PositionConfDict.Add(ret[i][0], new PositionConf(ret[i]));
            }
        }

        public static PositionConf GetConfById(string id)
        {
            if (PositionConfDict.ContainsKey(id))
            {
                return PositionConfDict[id];
            }

            return null;
        }

        public static PositionConf GetConfByServerId(int serverid)
        {
            foreach (var keyValuePair in PositionConfDict)
            {
                if (keyValuePair.Value.sortLineServerId == serverid)
                {
                    return keyValuePair.Value;
                }
            }

            return null;
        }
    }
}